package com.datas.beans;

import lombok.Getter;
import lombok.Setter;
import org.json.JSONObject;

import java.util.Collection;

/**
 * Created by jige0727 on 2016/12/31.
 * 分页信息用 setData setTotal 设置数据
 * 用getFinalData返回数据
 * datatables 的分页bean
 */
@Getter
@Setter
public class DatatablesPager {
    Collection<Integer> idsByAuthority; //用来过滤数据权限
    String currentUsername; //用来判断系统管理员管理员-> admin

    Integer draw = 0;
    Long recordsTotal = 0L;
    Long recordsFiltered = 0L;
    Integer start = 0;
    Integer length = 10;
    Object data;
    DatatablesPagerSearch search;

    public void setTotal(Long count) {
        recordsTotal = recordsFiltered = count;
    }

    public String getSearchValue() {
        if (search == null) {
            return "%%";
        }
//        LoggerFactory.getLogger(getClass()).info("search -> " + new JSONObject(search));
        return "%" + search.value.replaceAll(" ", "%") + "%";
    }

    public JSONObject getFinalData() {
        return new JSONObject()
                .put("draw", draw)
                .put("recordsTotal", recordsTotal)
                .put("recordsFiltered", recordsFiltered)
                .put("data", data);
    }

    @Override
    public String toString() {
        return getFinalData().toString();
    }
}
